<?php

class Dbi {
	public static $Connection;

	public static $Address;
	public static $Username;
	public static $Password;
	public static $Database;

	private static $_requests = array();

	public static function GetResult($query) {
		if ( !self::$Connection ) {
			self::$Connection = mysql_connect(self::$Address, self::$Username, self::$Password);
			mysql_select_db(self::$Database);
		}

		$result = mysql_query($query);

		self::$_requests[] = new DbiRequest($query, $result);
		return $result;
	}

	public static function ShowRequests() {
		?><table border="1" cellpadding="2" cellspacing="2"><?
		foreach ( self::$_requests as $request ) {
			?><tr>
				<th>Query</th>
			</tr>
			<tr>
				<td><pre><?=$request->Query?></pre></td>
			</tr>
			<? if ($request->Error): ?>
				<tr>
					<th>Error</th>
				</tr>
				<tr>
					<td><pre><?=$request->Error?></pre></td>
				</tr>
			<? else:?>
				<tr>
					<th>Result</th>
				</tr>
				<tr>
					<td><?=$request->Result?></td>
				</tr>
			<? endif;
		}
		?></table><?
	}
}

class DbiRequest {
	public $Query;
	public $Result;
	public $Error;
	public function __construct($query, $result) {
		$this->Query = $query;
		$this->Result = $result;
		$this->Error = mysql_error();
	}
}